﻿using CSharpTestProject.Utils;
using System;
using System.Data;
using System.Windows.Forms;

namespace CSharpTestProject.AdminFormFolder
{
    public partial class ModifyTeacherForm : Form
    {
        public _Sql sql;
        public ModifyTeacherForm()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox1.ReadOnly = false;
            textBox2.ReadOnly = true;
            textBox3.ReadOnly = true;
            button3.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string query = $"SELECT kcm FROM teachers WHERE zgh='{textBox1.Text}'";
            DataSet data = sql.SqlFind(query);
            try
            {
                textBox3.Text = data.Tables[0].Rows[0][0].ToString().Trim();
                textBox3.Text += ";";
                textBox2.Text = data.Tables[0].Rows[0][0].ToString().Trim();
                textBox3.ReadOnly = false;
                button3.Enabled = true;
            }
            catch
            {
                MessageBox.Show("查询失败！");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string course = textBox3.Text.Trim();
            if (!course.EndsWith(";"))
            {
                course += ";";
            }
            string s = $"UPDATE teachers SET kcm='{textBox3.Text}' WHERE zgh='{textBox1.Text}'";
            try
            {
                sql.ManageData(s);
                MessageBox.Show("修改成功！");
                Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show("修改失败！");
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
